package com.yang.spring.core.bean.definition;

import com.yang.spring.core.bean.enums.BeanTypeEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanDefinition implements Serializable {
    private Class beanClassType;

    private String beanName;

    private BeanTypeEnum beanType;

    private Object instance;

    private Map<String, BeanDefinition> dependencyBeanName2BeanDefinitionMap = new HashMap<>();

    private Map<String, Object> extendMap = new HashMap<>();

    public Map<String, BeanDefinition> getDependencyBeanName2BeanDefinitionMap() {
        return this.dependencyBeanName2BeanDefinitionMap;
    }

    public List<BeanDefinition> getDependencyBeanDefinitionList() {
        return new ArrayList<>(this.dependencyBeanName2BeanDefinitionMap.values());
    }

    public void putDependencyBeanDefinition(String dependencyBeanName, BeanDefinition beanDefinition) {
        dependencyBeanName2BeanDefinitionMap.put(dependencyBeanName, beanDefinition);
    }

    public Class getBeanClassType() {
        return beanClassType;
    }

    public void setBeanClassType(Class beanClassType) {
        this.beanClassType = beanClassType;
    }

    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof BeanDefinition)) {
            return false;
        }
        return o.hashCode() == this.hashCode();
    }

    @Override
    public int hashCode() {
        int beanClassTypeHashCode = beanClassType == null ? 0 : beanClassType.hashCode();
        int beanNameHashCode = beanName == null ? 0 : beanName.hashCode();
        return 31 * beanClassTypeHashCode + beanNameHashCode;
    }

    public BeanTypeEnum getBeanType() {
        return beanType;
    }

    public void setBeanType(BeanTypeEnum beanType) {
        this.beanType = beanType;
    }

    public void putExtend(String key, Object value) {
        this.extendMap.put(key, value);
    }

    public Object getExtend(String key) {
        return this.extendMap.get(key);
    }

    public Object getInstance() {
        return instance;
    }

    public void setInstance(Object instance) {
        this.instance = instance;
    }

    @Override
    public String toString() {
        return String.format("BeanDefinition{beanClassType:%s, beanName:%s, dependencyBeanNames:%s}", this.beanClassType,
                this.beanName, dependencyBeanName2BeanDefinitionMap.keySet().toString());
    }

    public boolean haveInstance() {
        return this.instance != null;
    }
}
